package com.itheima.app.manager;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import com.itheima.vo.SoundVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Struct;
import java.time.Duration;

@Component
public class SoundManager {
    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private SoundService soundService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //这里不用缓存了
    public void publishSound(MultipartFile soundFile) throws IOException {
    //获取userId
        Long userId = UserHolder.get().getId();
        //封装上传fastDFS
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), FileUtil.extName(soundFile.getOriginalFilename()), null);
        //获取连接地址url
        String soundUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        System.out.println(soundUrl);//暂时输出测试一下
        //封装 Sound
        Sound sound = new Sound();
        sound.setCreated(System.currentTimeMillis());
        sound.setUserId(userId);
        sound.setSoundUrl(soundUrl);

        soundService.publishSound(sound);
    }
//接收音频
    public ResponseEntity findSound() {
        Long userId = UserHolder.get().getId();
        //先从redis中查剩余次数
        String countStr =stringRedisTemplate.opsForValue().get("cishu" + userId);

        //如果第一次接收音频 设置次数为“10”
        if (StrUtil.isEmpty(countStr)) {
            countStr = "10";
        }
        Integer count = Integer.parseInt(countStr);



        count = count - 1;//次数减一
        //如果次数没有了 直接把count 置为0
        if (count<1) {
            count=0;
            SoundVo soundVo = new SoundVo();
            soundVo.setRemainingTimes(count);

            return ResponseEntity.ok(soundVo);
        }

        //把新次数返回redis
        stringRedisTemplate.opsForValue().set("cishu" + userId,count.toString(), Duration.ofDays(1));

        //rpc调用
        Sound sound = soundService.findSound(userId);
        //获取userInfo
        UserInfo userInfo = userInfoService.findByUserId(sound.getUserId());
        //封装soundVo
        SoundVo soundVo = new SoundVo();
        soundVo.setUserInfo(userInfo);
        soundVo.setSoundUrl(sound.getSoundUrl());



        soundVo.setRemainingTimes(count);

        return ResponseEntity.ok(soundVo);
    }
}
